﻿using Applay;
using Applay.Web;
using Example.Application.Account;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace Example.Web.Controllers
{
    public class AccountController : Controller
    {
        private readonly AccountService accountService;

        public AccountController(AccountService accountService)
        {
            this.accountService = accountService; 
        }

        [HttpGet]
        public ActionResult LogIn()
        {
            return View(new LogInModel());
        }

        [HttpPost]
        public ActionResult LogIn(LogInModel model)
        {
            if (this.accountService.LogIn(model))
            {
                FormsAuthentication.SetAuthCookie(model.Email, true);

                return Redirect(FormsAuthentication.GetRedirectUrl(model.Email, true));
            }

            return View(model);
        }

        [HttpGet]
        public ActionResult LogOut()
        {
            FormsAuthentication.SignOut();

            return Redirect(FormsAuthentication.DefaultUrl);
        }

        [HttpGet]
        public ActionResult Register()
        {
            return View(new RegisterModel());
        }

        [HttpPost]
        public ActionResult Register(RegisterModel model)
        {
            if (this.accountService.Register(model))
            {
                FormsAuthentication.SetAuthCookie(model.Email, true);

                return Redirect(FormsAuthentication.GetRedirectUrl(model.Email, true));
            }

            return View(model);
        }
    }

    

}
